package com.spboot.exts.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.AuthorizationException;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.alibaba.fastjson.JSON;
import com.spboot.exts.entity.ajax.AjaxJsonResult;
import com.spboot.exts.exception.AjaxAuthorizationException;

import jdk.nashorn.internal.runtime.JSONFunctions;

/**
 * 异常处理
 * 
 * @author Administrator
 *
 */
@ControllerAdvice
public class SysExceptionAdvice {

	/**
	 * 用户未授权
	 * 
	 * @param e
	 * @param model
	 * @return
	 */
	@ResponseStatus(code = HttpStatus.UNAUTHORIZED)
	@ExceptionHandler(value = { AuthorizationException.class })
	public String noAuth(Exception e, Model model,
			HttpServletRequest request,
			HttpServletResponse response)
	{
		
		 String accept = request.getHeader("Accept");
		 if("*/*".equals(accept)){
			 //ajax
			
			 model.addAttribute("jsonObj", JSON.toJSONString(AjaxJsonResult.error("用户没有权限操作该功能!",null)));
			 return "ajaxunauthc";
		 }
//		model.addAttribute("error", e.getMessage());
		return "unauthc";
	}
}
